package com.dna.common.io.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.dna.common.io.StreamJack;
import junit.framework.TestCase;

public class StreamJackTestCase extends TestCase {

	private StreamJack jack;
	private StreamJack jack2;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		if(jack!=null)
			jack.close();
		if(jack2!=null)
			jack2.close();
	}
	
	public void testBinding1to1() {
		String message = "test message";
		byte[] msg = message.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(msg); 
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		jack = new StreamJack(in, out);
		jack.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals("Checking that in is equals out", message, out.toString());
	}
	
	public void testBinding1toN() {
		String message = "test message";
		byte[] msg = message.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(msg); 
		ByteArrayOutputStream out1 = new ByteArrayOutputStream();
		ByteArrayOutputStream out2 = new ByteArrayOutputStream();
		jack = new StreamJack(in, out1, out2);
		jack.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals("Checking that in is equals out1",message, out1.toString());
		assertEquals("Checking that in is equals out2",message, out2.toString());
	}
	
	public void testTracing() {
		String message = "test message";
		byte[] msg = message.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(msg);
		ByteArrayOutputStream out1 = new ByteArrayOutputStream();
		ByteArrayOutputStream out2 = new ByteArrayOutputStream();
		StreamJack.traceInputs(true);
		jack = new StreamJack(in, out1);
		jack2 = new StreamJack(in, out2);
		jack.start();
		jack2.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals("Checking that in is equals out1",message, out1.toString());
		assertEquals("Checking that in is equals out2",message, out2.toString());
	}
}
